package com.ruoyi.web.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.web.area.domain.Continents;
import com.ruoyi.web.area.domain.Country;
import com.ruoyi.web.area.service.IContinentsService;
import com.ruoyi.web.area.service.ICountryService;
import com.ruoyi.web.entity.dto.EpidemicDataDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("dataTask")
public class RyTask {

    @Resource
    IContinentsService continentsService;

    @Resource
    ICountryService countryService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法2");
    }

    public void recordData() {
        System.out.println("执行每天疫情数据");
        RestTemplate restTemplate = new RestTemplate();
        //请求参数
        Map<String, String> params = new HashMap<>();
        params.put("key", "d334721cf6eba2d619a5855420ec352c");
        //日期
        params.put("date", DateUtil.today());
        String url = "https://api.tianapi.com/ncovabroad/index?key={key}&date={date}";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, params);
        JSONObject jsonObject = JSONObject.parseObject(responseEntity.getBody());
        if (jsonObject != null) {
            if ("200".equals(jsonObject.getString("code"))) {
                JSONArray newsList = jsonObject.getJSONArray("newslist");
                List<EpidemicDataDTO> epidemicDataDTOList = newsList.toJavaList(EpidemicDataDTO.class);
                Map<String, List<EpidemicDataDTO>> map = epidemicDataDTOList.stream()
                        .filter(item -> StringUtils.isNoneBlank(item.getContinents()))
                        .collect(Collectors.groupingBy(EpidemicDataDTO::getContinents));
                for (Map.Entry<String, List<EpidemicDataDTO>> entry : map.entrySet()) {
                    Continents continents = new Continents();
                    continents.setName(entry.getKey());
                    List<EpidemicDataDTO> dataList = entry.getValue();
                    List<Continents> resultList = continentsService.selectContinentsList(continents);
                    if(CollectionUtil.isEmpty(resultList)){
                        continents.setCountryNum((long) dataList.size());
                        continentsService.insertContinents(continents);
                    }else {
                        Continents continent = resultList.get(0);
                        for (EpidemicDataDTO dataDTO : dataList) {
                            Country country = new Country();
                            country.setName(dataDTO.getProvinceName());
                            List<Country> countryList = countryService.selectCountryList(country);
                            if(CollectionUtil.isEmpty(countryList)){
                                country.setContinentid(continent.getId());
                                country.setLocationid(dataDTO.getLocationId());
                                country.setCode(dataDTO.getCountryShortCode());
                                countryService.insertCountry(country);
                            }
                        }
                    }
                }
                System.out.println(epidemicDataDTOList);
            }
        }
    }
}
